
export interface IPager {
  rowsPerPageOptions?: number[];
  pageSize?: number;
  pageLinks?: number;
  totalCount?: number;
  first?: number;
}

export class Pager {
  constructor(config: IPager) {
    Object.keys(config).forEach(field => {
      this[field] = config[field];
    });
  }
  private _first = 0;
  private _maxSize= 999;
  private _pageIndex: number;
  private _rowsPerPageOptions: number[];
  public enabled= true;
  public totalCount: number;
  public pageSize: number;
  public pageLinks: number;

  public Disabled() {
    this.enabled = false;
    this.pageSize = this._maxSize;
  }
  set first(value){
    this._first = value;
  }
  get pageIndex(){
    this._pageIndex = (this._first / this.pageSize) + 1;
    return this._pageIndex;
  }
  set pageIndex(value){
    this._pageIndex = value;
  }
  get upBtnActive(){
    if (this.pageIndex > 1) {
      return true;
    } else {
      return false;
    }
  }
  get downBtnActive(){
    if ( this.totalCount > this.pageSize * this.pageIndex) {
      return true;
    } else {
      return false;
    }
  }
  get rowsPerPageOptions(){
    return this._rowsPerPageOptions;
  }
  set rowsPerPageOptions(value){
    this._rowsPerPageOptions = [];
    // 当pageSize的值不在_rowsPerPageOptions中，则将其添加到_rowsPerPageOptions的第一个元素
    if (value.indexOf(this.pageSize) < 0 ) {
      this._rowsPerPageOptions.push(this.pageSize);
    }
    value.forEach(item => this._rowsPerPageOptions.push(item));
  }
}

